<script setup lang="ts">
import { ref, computed } from 'vue'
import type { Ref } from 'vue'
const loginusername: Ref<string> = ref('')
const loginpassword: Ref<string> = ref('')

const flag = computed(() => {
    return /^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(loginusername.value) != true
}
)
//登录接口
import { loginFn } from '@/api/register'
import { useRouter,useRoute } from 'vue-router';
import { showToast } from 'vant';
const router = useRouter()
import { useUserStore } from '@/stores/user';
const user = useUserStore()
const route=useRoute()
function onSubmit() {
    //提交的方法
    loginFn({
        loginname: loginusername.value,
        password: loginpassword.value
    }).then(res => {
        console.log(21111111, res,loginpassword.value)
        if (res.data.token) {
            console.log(333333333);
            user.changeUserState({
                token: res.data.token,
                userid: res.data.userid,
                isLogin: true
            })
            //如果从详情页面进入的登录，那么就回到详情页面
            if(route.query.type=='back'){
                router.back()
            }else{

                router.push('/')
            }
        } else {
            showToast(res.data.message)
        }
    })
}
</script>

<template>
    <main class="box">
        <header class="header">
            头部-登录

        </header>
        <div class="content">
            <van-form @submit="onSubmit">
                <van-cell-group inset style="height: 120px;">
                    <van-field v-model="loginusername" name="用户名" label="用户名" placeholder="用户名"
                        :rules="[{ required: true, message: '请填写用户名' }]" />
                    <van-field v-model="loginpassword" type="password" name="密码" label="密码" placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]" />
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button :disabled="flag" round block type="success" native-type="submit">
                        登录
                    </van-button>
                </div>
                <div style="margin: 16px;">
                    <van-button round block color="#f66" native-type="submit" @click="$router.push('/register')">
                        去注册
                    </van-button>
                </div>
            </van-form>
        </div>
    </main>
</template>
<style lang="scss" scoped>
.content {
    margin-top: 60px
}
</style>